package com.controller;

import com.entity.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    private final String PROVIDER_URL = "http://localhost:8001";

    @Autowired private RestTemplate restTemplate;

    @GetMapping("/{id}")
    public Payment getById(@PathVariable String id) {
        ResponseEntity<Payment> response = restTemplate.getForEntity(PROVIDER_URL + "/payment/" + id, Payment.class);
        return response.getBody();
    }

    @GetMapping
    public List<Payment> getById() {
        ResponseEntity<Payment[]> response = restTemplate.getForEntity(PROVIDER_URL + "/payment", Payment[].class);
        return response.getBody() == null ? new ArrayList<>() : Arrays.asList(response.getBody());
    }

    @PostMapping
    public void add(@RequestBody Payment payment) {
        restTemplate.postForEntity(PROVIDER_URL + "/payment", payment, Void.class);
    }
}
